/*************************************
 *
 ************************************/


/**
 *
 */
function TestFireObjectWrapper( parent, objectToWrap, objectName, isSelectedObjectWrapper, testFire )
{
    this.doInit.apply(this, arguments);
    this.displayValue = objectName;
    this.parent = parent;
    this.wrappedObject = objectToWrap;
    this.name = objectName;
    this.isSelectedObjectWrapper = isSelectedObjectWrapper;
    this.testFire = testFire;
}

/**
 * Define the prototype.
 */
TestFireObjectWrapper.prototype = domplate(TestFireObject, {

    typesToSkip: new Array("function"),
    options: {},
    isTestFireObject: 1,
    init: function()
    {
    },


    getAssertionMenuItems: function( object, target )
    {

        try
        {
            var cfo = this.testFire.getSelectedTest().testTasks;
            var finder = HtmlFinder.getHtmlFinder(this.wrappedObject);
            var asserts = new Array();
            if ( finder && cfo )
            {
                var itemNames = new Array();
                for ( var name in finder )
                {
                    itemNames.push(name);
                }
                itemNames.sort();
                for ( var i = 0; i < itemNames.length; i++ )
                {
                    var item = finder[itemNames[i]];
                    if ( item && item.assertable && item.assertable != 0 )
                    {
                        asserts.push(HtmlFinder.createMenuItem(finder, itemNames[i], this.wrappedObject, cfo));
                    }
                }
            }
            return asserts;
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }

    },



    getActionMenuItems: function( object, target )
    {
        try
        {
            var cfo = this.testFire.getSelectedTest().testTasks;
            var finder = HtmlFinder.getHtmlFinder(this.wrappedObject);
            if(finder == null)
            {
                return new Array();
            }
            var cmds = new Array();
            if ( finder && cfo )
            {
                var itemNames = new Array();
                for ( var name in finder )
                {
                    itemNames.push(name);
                }
                itemNames.sort();
                for ( var i = 0; i < itemNames.length; i++ )
                {
                    var item = finder[itemNames[i]];
                    if ( item && item.actionable && item.actionable != 0 )
                    {
                        cmds.push(HtmlFinder.createMenuItem(finder, itemNames[i], this.wrappedObject, cfo));
                    }
                }
            }
            return cmds;
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }

    },

    getContextMenuItems: function( object, target )
    {
        var items = [];
        try
        {

            var isTop = !this.parent || (this.parent && !(this.parent instanceof TestFireObjectWrapper));
            if ( !isTop )
            {
                items.push(
                    "-",
                { nol10n: 1, label: "Set as Selection Object", command: FBL.bindFixed(this.setAsSelectedObject,
                    this, this.wrappedObject) });
            }

            var finder = HtmlFinder.getHtmlFinder(this.wrappedObject);
            if(finder == null)
            {
                return new Array();
            }
            var cfo = Firebug.TestFire.selectedTasksList;

            if ( finder && isTop && cfo )
            {
                var asserts = this.getAssertionMenuItems(object, target);
                var cmds = this.getCommandMenuItems(object, target);

                items.push("-");
                items.push({ label: "Assert", menuItems: asserts });
                items.push("-");
                items.push({ label: "Command", menuItems: cmds });
            }

            if ( !this.isSelectedObjectWrapper )
            {
                items.push(
                { nol10n: 1, label: "Use This", command: FBL.bindFixed(this.replaceTopWith,
                    this, this.wrappedObject) });

            }

        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }

        return items;
    },

    testMethodForFinder: function( htmlFinder )
    {
        var cfo = Firebug.TestFireExtension.getState().selectedTasksList;
        if ( cfo )
        {

        }

        alert(FBL.getElementXPath(this.wrappedObject));
    },

    setAsSelectedObject: function( object )
    {
        FirebugContext.getPanel("TestFireTreePanel").setTargetNode(object);
    },

    replaceTopWith: function( object )
    {
        var obj = this;
        var isAlreadyTop = true;
        while ( obj.parent && obj.parent instanceof TestFireObjectWrapper )
        {
            isAlreadyTop == false;
            obj = obj.parent;
        }
        if ( !isAlreadyTop )
        {
            obj.setTargetObject(object);
        }
    },

/**
 * {@inheritdoc}
 * @return
 */
    getChildrenToDisplay: function()
    {
        try
        {
            if ( !Firebug )
            {
                return {};
            }
        }
        catch( ex )
        {
            return {};
        }
        var ary = new Array();
        var x = this;
        for ( var name in this.wrappedObject )
        {
            if ( !(name in x) )
            {
                try
                {
                    if ( this.typesToSkip.indexOf(typeof(this.wrappedObject[name])) >= 0 )
                    {
                        continue;
                    }
                    var wrap = new TestFireObjectWrapper(this, this.wrappedObject[name], name, this.isSelectedObjectWrapper);
                    ary.push({target: this.wrappedObject, object: wrap, name: name});
                }
                catch( ex )
                {
                    if ( ex.result && ex.result == 2147500037 )
                    {
                        continue;
                        // access can throw exception; ignore and continue.
                    }
                    throw(ex);
                }
            }
        }
        return ary;

    },


    setTargetObject: function( objectToWrap )
    {
        this.wrappedObject = objectToWrap;
        this.displayValue = objectToWrap.toString();
        FirebugContext.getPanel("TestFireTreePanel").rebuild();
    },

    getWrappedObject: function()
    {
        return this.wrappedObject;
    },

    getFinderSteps: function()
    {

    }








});